const sdk = require('./sdk');

module.exports.list = async function (data) {
  data.data.query = {
    ...data.data.query,
    // min_access_level: 18,
  };
  delete data.data.query.min_access_level;
  data.data.json = {
    ...data.data.json,
    PageNumber: '1',
    PageSize: '100',
    Action: 'DescribeMyDepots',
  };
  const codingMyDepots = await sdk.request('/DescribeMyDepots', 'POST', data);
  console.log('----------codingMyDepots: ', codingMyDepots);
  let resp = codingMyDepots?.Response?.Payload?.Depots?.map((one) => {
    one.name_with_namespace = one.HttpsUrl.split('.net/')[1].replace('.git', '');
    one.web_url = one.WebUrl; // 仓库的浏览器访问地址
    one.id = one.Id; // 仓库id
    one.ssh_url_to_repo = one.SshUrl;
    one.https_url_to_repo = one.HttpsUrl;
    one.path_with_namespace = one.HttpsUrl.split('.net/')[1].replace('.git', ''); // 具体的仓库路径，该值是唯一的，tapd上实测是拿这个当table的key，不设置的话，一勾选就是全部数据
    // one.review_enabled = false; // 关闭tapd的评审功能 tapd点击评审会跳转到git/reviews/new  这个页面coding没有，后期可以在vcs上做个对应的路由重定向
    // one.merge_requests_enabled = false; // 关闭tapd的合并请求功能 因为tapd默认会跳转到/p/joosure/d/sen-test/git/merge_requests/new?sourceBranch=dev页面，coding这边没有这个页面，
    return one;
  });
  // 因为openapi不支持查询，所以前端做一下过滤
  if (data.data.query?.search) {
    resp = resp.filter(one => (one.name_with_namespace.toLowerCase()).includes(data.data.query.search.toLowerCase()));
  }

  console.log('-----------获取到的仓库列表长度', resp?.length);
  return resp;
};

module.exports.get = async function (data) {
  // console.log(`execute project.get : ${JSON.stringify(data)}`);
  const { query } = data.data;
  data.data.json = {
    DepotId: query.project_id,
    Action: 'DescribeGitDepot',
  };
  const tempRes = await sdk.request('/DescribeGitDepot', 'POST', data);

  const resp = tempRes?.Response?.Depot;
  console.log('----------------resp.HttpsUrl: ', resp.HttpsUrl);
  const finalRes = {
    id: resp.Id,
    description: resp.Description,
    name: resp.Name,
    name_with_namespace: resp.HttpsUrl.split('.net/')[1].replace('.git', ''),
    path: resp.HttpsUrl.split('.net/')[1].replace('.git', ''),
    path_with_namespace: resp.HttpsUrl.split('.net/')[1].replace('.git', ''),
    default_branch: resp.DefaultBranch,
    ssh_url_to_repo: resp.SshUrl,
    https_url_to_repo: resp.HttpsUrl,
    web_url: resp.WebUrl,
    created_at: resp.CreatedAt,
    last_activity_at: resp.LastPushAt,
    // review_enabled: false, // 关闭tapd的评审功能 tapd点击评审会跳转到git/reviews/new  这个页面coding没有，后期可以在vcs上做个对应的路由重定向
    // merge_requests_enabled: false, // 关闭tapd的合并请求功能 因为tapd默认会跳转到/p/joosure/d/sen-test/git/merge_requests/new?sourceBranch=dev页面，coding这边没有这个页面，
    // creator_id: 249013, // 创建仓库人的id
  };
  return finalRes;
};
